package com.stary.springmvc.web.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义拦截器
 *  1.添加组件注解Component
 *  2.实现接口HandlerInterceptor
 * @author: Guan
 **/
@Component
public class MyInterceptor01 implements HandlerInterceptor {
    /**
     * 解读：
     *   1.preHandle()：在目标方法前执行
     *   2.该方法可以获取到 HttpServletRequest request, HttpServletResponse response, Object handler
     *   3.返回true继续执行目标方法
     *   4.如果返回false，则不再执行目标方法。
     *   5.可以根据业务进行拦截，并制定跳转到哪个页面,如果没进行操作，则返回空白页面
     * @param request current HTTP request
     * @param response current HTTP response
     * @param handler chosen handler to execute, for type and/or instance evaluation
     *                com.stary.springmvc.web.interceptor.TestInterceptor#hi1() --> 执行的目标方法
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("-- MyInterceptor01 -- preHandle");
        String key = request.getParameter("key");
        if(key.equals("病毒")){
            request.getRequestDispatcher("/WEB-INF/pages/warning.jsp").forward(request,response);
            return false;
        }
        return true;
        // return false;
    }

    /**
     * 解读：
     *  1.在目标方法执行后，会执行postHandle()方法
     *  2.该方法可以获取到ModelAndView对象
     * @param request current HTTP request
     * @param response current HTTP response
     * @param handler the handler (or {@link HandlerMethod}) that started asynchronous
     * execution, for type and/or instance examination
     * @param modelAndView the {@code ModelAndView} that the handler returned
     * (can also be {@code null})
     *                     ModelAndView [view="success"; model={}] --> 视图 + 模型（数据）
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("-- MyInterceptor01 -- postHandle");
    }

    /**
     * 解读：
     *  1,.afterCompletion()在视图渲染后执行，可以进行一些资源清理工作
     * @param request current HTTP request
     * @param response current HTTP response
     * @param handler the handler (or {@link HandlerMethod}) that started asynchronous
     * execution, for type and/or instance examination
     * @param ex any exception thrown on handler execution, if any; this does not
     * include exceptions that have been handled through an exception resolver
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("-- MyInterceptor01 -- afterCompletion");
    }
}
